<?php
class slideshow_options_ControllerModuleSlideshow extends ControllerModuleSlideshow {

	private $error = array();


	private function is_a_valid_number( &$str, $maxval, $minval=0 ) {
		$j = strlen($str);
		$unsigned = ($j > 0);
		for ($i=0; $i<$j; $i+=1) {
			if ($str[$i] < '0') {
				return FALSE;
			}
			if ($str[$i] > '9') {
				return FALSE;
			}
		}
		if ($unsigned) {
			if (((int)$str < $minval) || ((int)$str > $maxval)) {
				return FALSE;
			}
			$str = strval((int)$str);
			return TRUE;
		}
		return FALSE;
	}


	// overridden method
	protected function validate() {
		$ok = parent::validate();

		if (isset($this->request->post['slideshow_module'])) {
			foreach ($this->request->post['slideshow_module'] as $key => $value) {
				if (!empty($value['animspeed'])) {
					if (!$this->is_a_valid_number( $value['animspeed'], 10000, 100 )) {
						$this->error['animspeed'][$key] = $this->language->get('error_animspeed');
					}
				}
				if (!empty($value['pausetime'])) {
					if (!$this->is_a_valid_number( $value['pausetime'], 10000, 100 )) {
						$this->error['pausetime'][$key] = $this->language->get('error_pausetime');
					}
				}
			}
		}

		return ($this->error) ? FALSE : $ok;
	}


	// overridden method
	protected function preRender( $templateBuffer ) {
		if (!$this->template == 'module/slideshow.tpl') {
			return parent::preRender( $templateBuffer );
		}

		// add support for additional variables
		$this->data['entry_effect'] = $this->language->get('entry_effect');
		$this->data['entry_animspeed'] = $this->language->get('entry_animspeed');
		$this->data['entry_pausetime'] = $this->language->get('entry_pausetime');
		$this->data['effects'] = array(
			'sliceDown',
			'sliceDownLeft',
			'sliceUp',
			'sliceUpLeft',
			'sliceUpDown',
			'sliceUpDownLeft',
			'fold',
			'fade',
			'random',
			'slideInRight',
			'slideInLeft',
			'boxRandom',
			'boxRain',
			'boxRainReverse',
			'boxRainGrow',
			'boxRainGrowReverse'
		);
		foreach ($this->data['modules'] as $key=>$module) {
			if (!isset($module['effect'])) {
				$this->data['modules'][$key]['effect'] = 'random';
			}
			if (!isset($module['animspeed'])) {
				$this->data['modules'][$key]['animspeed'] = '500';
			}
			if (!isset($module['pausetime'])) {
				$this->data['modules'][$key]['pausetime'] = '3000';
			}
		}

		// add additional error variables
		if (isset($this->error['animspeed'])) {
			$this->data['error_animspeed'] = $this->error['animspeed'];
		} else {
			$this->data['error_animspeed'] = array();
		}
		if (isset($this->error['pausetime'])) {
			$this->data['error_pausetime'] = $this->error['pausetime'];
		} else {
			$this->data['error_pausetime'] = array();
		}

		// modify template buffer to support additional variables
		$this->load->helper( 'modifier' );
		$search = '<td class="left"><?php echo $entry_dimension; ?></td>';
		$add  = '<td class="left"><?php echo $entry_dimension; ?></td>';
		$add .= '<td class="left"><?php echo $entry_effect; ?></td>';
		$add .= '<td class="left"><?php echo $entry_animspeed; ?></td>';
		$add .= '<td class="left"><?php echo $entry_pausetime; ?></td>';
		$templateBuffer = Modifier::modifyStringBuffer( $templateBuffer,$search,$add,'replace' );
		$search = '<td class="left"><select name="slideshow_module[<?php echo $module_row; ?>][layout_id]">';
		$add  = '              <td class="left"><select name="slideshow_module[<?php echo $module_row; ?>][effect]">'."\n";
		$add .= '                  <?php foreach ($effects as $effect) { ?>'."\n";
		$add .= '                  <?php if ($effect == $module[\'effect\']) { ?>'."\n";
		$add .= '                  <option value="<?php echo $effect; ?>" selected="selected"><?php echo $effect; ?></option>'."\n";
		$add .= '                  <?php } else { ?>'."\n";
		$add .= '                  <option value="<?php echo $effect; ?>"><?php echo $effect; ?></option>'."\n";
		$add .= '                  <?php } ?>'."\n";
		$add .= '                  <?php } ?>'."\n";
		$add .= '                </select></td>'."\n";
		$add .= '              <td class="left"><input type="text" name="slideshow_module[<?php echo $module_row; ?>][animspeed]" value="<?php echo $module[\'animspeed\']; ?>" size="3" />'."\n";
		$add .= '                <?php if (isset($error_animspeed[$module_row])) { ?>'."\n";
		$add .= '                <span class="error"><?php echo $error_animspeed[$module_row]; ?></span>'."\n";
		$add .= '                <?php } ?></td>'."\n";
		$add .= '              <td class="left"><input type="text" name="slideshow_module[<?php echo $module_row; ?>][pausetime]" value="<?php echo $module[\'pausetime\']; ?>" size="3" />'."\n";
		$add .= '                <?php if (isset($error_pausetime[$module_row])) { ?>'."\n";
		$add .= '                <span class="error"><?php echo $error_pausetime[$module_row]; ?></span>'."\n";
		$add .= '                <?php } ?></td>'."\n";
		$templateBuffer = Modifier::modifyStringBuffer( $templateBuffer,$search,$add,'before' );
		$search = '<td colspan="6"></td>';
		$add = '<td colspan="9"></td>';
		$templateBuffer = Modifier::modifyStringBuffer( $templateBuffer,$search,$add,'replace' );
		$search = 'html += \'    <td class="left"><select name="slideshow_module[\' + module_row + \'][layout_id]">\';';
		$add  = '	html += \'    <td class="left"><select name="slideshow_module[\' + module_row + \'][effect]">\';'."\n";
		$add .= '	<?php foreach ($effects as $effect) { ?>'."\n";
		$add .= '	html += \'      <option value="<?php echo $effect; ?>"><?php echo addslashes($effect); ?></option>\';'."\n";
		$add .= '	<?php } ?>'."\n";
		$add .= '	html += \'    </select></td>\';'."\n";
		$add .= '	html += \'    <td class="left"><input type="text" name="slideshow_module[\' + module_row + \'][animspeed]" value="500" size="4" /></td>\';'."\n";
		$add .= '	html += \'    <td class="left"><input type="text" name="slideshow_module[\' + module_row + \'][pausetime]" value="3000" size="4" /></td>\';'."\n";
		$templateBuffer = Modifier::modifyStringBuffer( $templateBuffer,$search,$add,'before' );

		return parent::preRender( $templateBuffer );
	}
}
?>